﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Windows.Input;
using BotModels;
using BotTalkLibrary;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;

namespace AllBots.ViewModel
{
    public class FamousBotsViewModel : ViewModelBase
    {
        #region Private fields

        private string botsFilePath = "/AllBots;component/famousbots.txt";
        private RequestMaker caller;

        #endregion

        #region Commands

        private RelayCommand<Bot> sendBotIdCommand;
        public ICommand SendBotIdCommand
        {
            get
            {
                if (sendBotIdCommand == null)
                {
                    sendBotIdCommand =
                        new RelayCommand<Bot>(
                            bot => 
                            {
                                IdSent = true;
                                Messenger.Default.Send<GenericMessage<Bot>>(new GenericMessage<Bot>(bot));
                                IdSent = false;
                            });
                }

                return sendBotIdCommand;
            }
        }

        #endregion

        #region INPC

        private bool idSent;
        public bool IdSent
        {
            get { return idSent; }
            set
            {
                if (idSent == value)
                {
                    return;
                }

                idSent = value;
                RaisePropertyChanged("IdSent");
            }
        }

        private ObservableCollection<Bot> famousBots; 
        public ObservableCollection<Bot> FamousBots { get { return famousBots; } set
        {
            if (value == famousBots)
                return;
            famousBots = value;
            RaisePropertyChanged("FamousBots");
        } }

        private bool botsLoading;
        public bool BotsLoading
        {
            get { return botsLoading; }
            set
            {
                if (value == botsLoading)
                    return;
                botsLoading = value;
                RaisePropertyChanged("BotsLoading");
            }
        }

        #endregion

        /// <summary>
        /// Initializes a new instance of the FamousBotsViewModel class.
        /// </summary>
        public FamousBotsViewModel()
        {
            if (IsInDesignMode)
            {
                FamousBots = new ObservableCollection<Bot>()
                {
                    new Bot() { Name = "Un bot" },
                    new Bot() { Name = "Un bot" },
                    new Bot() { Name = "Un bot" },
                    new Bot() { Name = "Un bot" },
                    new Bot() { Name = "Un bot" },
                    new Bot() { Name = "Un bot" },
                    new Bot() { Name = "Un bot" },
                    new Bot() { Name = "Un bot" },
                    new Bot() { Name = "Un bot" },
                };
            }
            else
            {
                FamousBots = new ObservableCollection<Bot>();
                caller = new RequestMaker();
                GetFamousBots();
            }
        }

        private void GetFamousBots()
        {
            //var stream = App.GetResourceStream(new Uri(botsFilePath, UriKind.Relative));
            //StreamReader reader = new StreamReader(stream.Stream);

            //string[] line = new string[2];
            //while (line != null)
            //{
            //    try
            //    {
            //        line = reader.ReadLine().Split(':');
            //        FamousBots.Add(new Bot() { ChatWithBotCommand = SendBotIdCommand, Id = line[0], Name = line[1] });
            //    }
            //    catch
            //    {
            //        line = null;
            //    }
            //}

            BotsLoading = true;
            caller.GetFamousBotsCompleted += GetFamousBotsCompleted;
            caller.GetFamousBots();
        }

        private void GetFamousBotsCompleted(object sender, GetFamousBotsEventArgs e)
        {
            if (e.Error == null)
            {
                BotsLoading = false;
                FamousBots = new ObservableCollection<Bot>(e.Bots);

                foreach (Bot bot in FamousBots)
                {
                    bot.ChatWithBotCommand = SendBotIdCommand;
                }
            }
        }

        public override void Cleanup()
        {
            FamousBots = null;

            base.Cleanup();
        }
    }
}